import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/',
      name: 'index',
      component: () => import('@/views/index')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/index/search')
    },
    {
      path: '/cityList',
      name: 'cityList',
      component: () => import('@/views/index/cityList')
    },
    {
      path: '/searchResult',
      name: 'searchResult',
      component: () => import('@/views/index/searchResult')
    },
    {
      path: '/hotel',
      name: 'hotel',
      component: () => import('@/views/index/hotel')
    },
    {
      path: '/room',
      name: 'room',
      component: () => import('@/views/index/room')
    },
    {
      path: '/roomOrder',
      name: 'roomOrder',
      component: () => import('@/views/index/roomOrder')
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('@/views/my')
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('@/views/my/order')
    },
    {
      path: '/orderdet',
      name: 'orderdet',
      component: () => import('@/views/my/orderdet')
    },
    {
      path: '/pay',
      name: 'pay',
      component: () => import('@/views/index/pay')
    },
    {
      path: '/record',
      name: 'record',
      component: () => import('@/views/my/record')
    },
    {
      path: '/mycard',
      name: 'mycard',
      component: () => import('@/views/my/mycard')
    },
    {
      path: '/buy',
      name: 'buy',
      component: () => import('@/views/my/buy')
    },
    {
      path: '/bind',
      name: 'bind',
      component: () => import('@/views/my/bind')
    },
    {
      path: '/addr',
      name: 'addr',
      component: () => import('@/views/my/addr')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/my/register')
    }
  ]
})
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('vuex')
  if (to.name === 'register' || to.path === '/') { //如果是首页或者登录页面直接进入页面
    next()
  } else {
    //如果是购买 优惠券 个人中心页面 走是否登录的验证流程
    const intercept = ['buy', 'mycard', 'my']
    const sign = intercept.indexOf(to.name)
    if (sign !== -1) {
      if (user) {
        const {
          init
        } = JSON.parse(localStorage.getItem('vuex'))
        const {
          userInfo
        } = init
        const {
          loginStatus
        } = userInfo
        if (loginStatus * 1 === 0) {
          next({
            name: 'register',
            query: from.path
          })
        } else {
          next()
        }
      } else {
        next({
          name: 'register',
          query: from.path
        })
      }
    } else {
      next()
    }
  }
})
export default router
